package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.BossspawnMapper;
import com.xgame.server.db.model.Bossspawn;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class BossspawnCsTool {
    @Resource
    private BossspawnMapper bossspawnMapper;

    //插入经验副本
    public Integer insertBossSpawn(Game.BossSpawnCS data) throws Exception {
        //1.删除本玩家的所有
        deleteBossSpawn(data);

        //2.插入
        List<Game.BossSpawnDataCS> items = data.getListList();
        int length = items.size();
        Integer count = new Integer(0);
        for (int i = 0; i < length; i++) {
            Game.BossSpawnDataCS one = items.get(i);
            Bossspawn map = new Bossspawn();
            map.setAccount(data.getAccount());
            map.setPlayerId(data.getPlayerID());
            map.setInstId(new Long(one.getInstId()));
            map.setChallengeCount(one.getChallengeCount());

            String lst = "";
            List<Game.BossSpawnDataOneCS> complst = one.getListList();
            int nlength = complst.size();
            for (int j = 0; j < nlength; j++) {
                if (j == nlength - 1) {
                    lst = lst + complst.get(j).getLongId() + ";" + complst.get(j).getChallengeHp();
                } else {
                    lst = lst + complst.get(j).getLongId() + ";" + complst.get(j).getChallengeHp() + "^";
                }
            }

            map.setDataList(lst);
            map.setBuyCount(one.getBuyCount());
            map.setIsOver(one.getIsOver() == true ? 1 : 0);
            lst = data.getAccount() + ";" + data.getPlayerID() + ";" + one.getBossRankCS().getRankValue() + ";" + one.getBossRankCS().getRewardId() + ";" + one.getBossRankCS().getLastRecord();
            map.setBossRank(lst);

            map.setWeekLastRecord(data.getWeekLastRecord());
            count += bossspawnMapper.insert(map);
        }

        return count;
    }

    //删除记录
    public Integer deleteBossSpawn(Game.BossSpawnCS data) throws Exception {
        EntityWrapper<Bossspawn> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        return bossspawnMapper.delete(userWrapper);
    }

    //查询记录
    public List<Bossspawn> selectBossSpawn(String account) throws Exception {
        EntityWrapper<Bossspawn> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return bossspawnMapper.selectList(userWrapper);
    }
}
